﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NBrightCore.common;

namespace DotNetNuke.Modules.NBrightEspace.Components
{
    public class SchedulerReport : DotNetNuke.Services.Scheduling.SchedulerClient 
    {


        public SchedulerReport(DotNetNuke.Services.Scheduling.ScheduleHistoryItem objScheduleHistoryItem) : base()
        {
            this.ScheduleHistoryItem = objScheduleHistoryItem;
        }

        public override void DoWork()
        {

            try
            {
                //notification that the event is progressing
                this.Progressing();
                //OPTIONAL

                var objCtrl = new EspaceController();

                //get last run date 
                string strSet = "";
                int LastRanDay = -1;

                var objSettings = objCtrl.GetByTypeCode("SETTINGS");

                var controlMapPath = objSettings.GetXmlProperty("genxml/hidden/controlmappath");
                if (controlMapPath == "")
                {
                    return;
                }

                strSet = objSettings.GetXmlProperty("genxml/textbox/schedulerlastrunday");
                if (Utils.IsNumeric(strSet))
                {
                    LastRanDay = Convert.ToInt32(strSet);
                }

                //(Only run once a day) 
                if (LastRanDay != DateTime.Now.Day)
                {
                    objCtrl.CreateHostingInvoices(DateTime.Now, controlMapPath);                    
                    // DO DOMAIN INVOICE
                    objCtrl.CreateDomainInvoices(DateTime.Now, controlMapPath);                    
                    
                    //run client email reports, on selected day of month.
                    if (objSettings.GetXmlProperty("genxml/textbox/txtclientemailday") == DateTime.Now.Day.ToString(""))
                    {
                        if (objSettings.GetXmlProperty("genxml/checkbox/chkactivereports") == "True")
                        {
                            objCtrl.SendClientEmailReport();
                        }
                    }

                    objSettings = objCtrl.GetByTypeCode("SETTINGS");
                    objSettings.SetXmlProperty("genxml/textbox/schedulerlastrunday", DateTime.Now.Day.ToString(""));
                    objCtrl.Update(objSettings);


                    // Send Email Report to Admin
                    var fromEmail = objSettings.GetXmlProperty("genxml/textbox/txtfromemail");
                    var adminEmail = objSettings.GetXmlProperty("genxml/textbox/txtadminemail");
                    var testEmail = objSettings.GetXmlProperty("genxml/textbox/txttestemail");
                    var tMode = objSettings.GetXmlProperty("genxml/textbox/schedulerlastrunday");
                    bool debugMode = tMode == "True";
                    if (debugMode)
                    {
                        adminEmail = testEmail;
                    }
                    var strEmail = "<div><h2>Hosting Invoice History</h2>";
                    strEmail += objSettings.GetXmlProperty("genxml/hidden/invoicereport");
                    strEmail += "</div>";
                    strEmail += "<div><h2>Domain Invoice History</h2>";
                    strEmail += objSettings.GetXmlProperty("genxml/hidden/domainreport");
                    strEmail += "</div>";
                    if (adminEmail != "")
                    {
                        DotNetNuke.Services.Mail.Mail.SendMail(fromEmail, adminEmail, "", "NBrightEspace Invoice Report", strEmail, "", "HTML", "", "", "", "");
                    }

                    //Remove unrequired USERDATAINFO
                    NBrightDNN.DnnUtils.PurgeDataBaseInfo(-1,-1,objCtrl,"USERDATAINFO");

                }

                //update the result to success since no exception was thrown
                this.ScheduleHistoryItem.Succeeded = true;
                this.ScheduleHistoryItem.AddLogNote("Espace Scheduler Completed.");

            }
            catch (Exception exc)
            {
                this.ScheduleHistoryItem.Succeeded = false;
                this.ScheduleHistoryItem.AddLogNote("Espace Scheduler failed. " + exc);
                this.ScheduleHistoryItem.Succeeded = false;
            }
        }

    }
}